﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;

namespace Forums
{
    public partial class HomePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //check for session object of cookies
            if (checkLogin() == true)
            {
                btnLogin_Clicked(sender, e);
            }
            
        }

        protected void btnLogin_Clicked(object sender, EventArgs e)
        {
            //create instance of the user control Menu.ascx
            UserControl menuControl = (UserControl)LoadControl("~/UserControls/Menu.ascx");

            //update the menuContentHolder
            Master.FindControl("menuContentHolder").Controls.Clear();
            Master.FindControl("menuContentHolder").Controls.Add(menuControl);

            Response.Redirect("ForumLandingPage.aspx");
        }

        private bool checkLogin()
        {
            //check to see if a session is already created
            if (Session["User"] != null)
            {
                return true;
            }        
            else
            {
                //check if a cookie exists to grab user info
                if (HttpContext.Current.Request.Cookies["User"] != null)
                {
                    //pass cookie info to the session object
                    Session["User"] = HttpContext.Current.Request.Cookies["User"].Value;
                    return true;
                }
                else
                {
                    return false;
                }                 
            }
        }
    }
}